﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace DesignLife.Admin
{
    public partial class CommentManager : System.Web.UI.Page
    {
        static bool statusTrue;
        static bool statusFalse;
        SqlConnection con = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                statusTrue = true;
                statusFalse = false;
                UpdateData(statusTrue, statusFalse);
            }
            fcEnabled();
        }

        private void fcEnabled()
        {
            if (hifcommentmanagermessagecontent.Value.Equals(""))
            {
                commentmanagerChange.Enabled = false;
                commentmanagerAdd.Enabled = true;
            }
            else
            {
                commentmanagerChange.Enabled = true;
                commentmanagerAdd.Enabled = false;
            }
        }

        private void UpdateData(bool status1, bool status2)
        {
            string sql = "select * from Comments";
            if (status1 != status2)
            {
                if (status1)
                    sql += " where Status = 'True'";
                else
                    sql += " where Status = 'False'";
            }
            DataTable tbcomment = new DataTable();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
            con.Open();

            SqlCommand com = new SqlCommand(sql, con);
            tbcomment.Load(com.ExecuteReader());
            commentmanager_item.DataSource = tbcomment;
            commentmanager_item.DataBind();
            cmRefesh();
            if (con != null)
                con.Close();
        }
        protected void commentmanager_item_ItemCommand(object source, DataListCommandEventArgs e)
        {
            hifcommentmanagermessagecontent.Value = ((Label)e.Item.FindControl("lbcommentmanager_Table_ID")).Text;
            txtcommentmanager_messagecontent.Text = ((Label)e.Item.FindControl("lbcommentmanager_Table_Message")).Text;
            txtcommentmanager_messageauthor.Text = ((Label)e.Item.FindControl("lbcommentmanager_Table_author")).Text;
            string status = ((Label)e.Item.FindControl("lbcommentmanager_Table_status")).Text;
            if (status.Equals("True"))
                rdocommentmanagermessagestatus.Checked = true;
            else
                rdocommentmanagermessagestatus.Checked = false;
            fcEnabled();
        }

        protected void commentmanagerAdd_Click(object sender, EventArgs e)
        {
            fcEnabled();
            if (hifcommentmanagermessagecontent.Value.Equals(""))
            {
                if (txtcommentmanager_messagecontent.Text.Equals(""))
                    commentmanagermessage.Text = "Message Is Not Null";
                else if (txtcommentmanager_messageauthor.Text.Equals(""))
                    commentmanagermessage.Text = "Author Is Not Null";
                else
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert into Comments values(@message,@author,@status)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@message", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@author", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@status", SqlDbType.Bit);
                    cmd.Parameters[0].Value = txtcommentmanager_messagecontent.Text;
                    cmd.Parameters[1].Value = txtcommentmanager_messageauthor.Text;
                    if (rdocommentmanagermessagestatus.Checked)
                        cmd.Parameters[2].Value = "True";
                    else
                        cmd.Parameters[2].Value = "False";
                    int process = cmd.ExecuteNonQuery();
                    if (process > 0)
                    {
                        txtcommentmanager_messageauthor.Text = "";
                        txtcommentmanager_messagecontent.Text = "";
                        rdocommentmanagermessagestatus.Checked = false;
                        if (con != null)
                            con.Close();
                        UpdateData(statusTrue, statusFalse);
                        commentmanagermessage.Text = "Insert Success";
                    }
                    else
                        commentmanagermessage.Text = "Insert Failed";
                }
            }
            else
                commentmanagermessage.Text = "Only Change Data";
        }

        protected void commentmanagerCancel_Click(object sender, EventArgs e)
        {
            cmRefesh();
        }

        private void cmRefesh()
        {
            commentmanagermessage.Text = "";
            hifcommentmanagermessagecontent.Value = "";
            txtcommentmanager_messagecontent.Text = "";
            txtcommentmanager_messageauthor.Text = "";
            rdocommentmanagermessagestatus.Checked = false;
            fcEnabled();
        }

        protected void commentmanager_Table_title_true_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbox = sender as CheckBox;
            statusTrue = cbox.Checked;
            if (!statusFalse & !statusTrue)
            {
                (sender as CheckBox).Checked = true;
                statusTrue = true;
            }
            UpdateData(statusTrue, statusFalse);
        }

        protected void commentmanager_Table_title_false_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbox = sender as CheckBox;
            statusFalse = cbox.Checked;
            if (!statusFalse & !statusTrue)
            {
                (sender as CheckBox).Checked = true;
                statusFalse = true;
            }
            UpdateData(statusTrue, statusFalse);
        }

        protected void commentmanagerChange_Click(object sender, EventArgs e)
        {
            if (!hifcommentmanagermessagecontent.Value.Equals("")) {
                if (txtcommentmanager_messagecontent.Text.Equals(""))
                    commentmanagermessage.Text = "Message Is Not Null";
                else if (txtcommentmanager_messageauthor.Text.Equals(""))
                    commentmanagermessage.Text = "Author Is Not Null";
                else
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Update Comments Set Message = @message, Authors=@author, Status = @status where CommentID = '" + hifcommentmanagermessagecontent.Value + "'";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@message", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@author", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@status", SqlDbType.Bit);
                    cmd.Parameters[0].Value = txtcommentmanager_messagecontent.Text;
                    cmd.Parameters[1].Value = txtcommentmanager_messageauthor.Text;
                    if (rdocommentmanagermessagestatus.Checked)
                        cmd.Parameters[2].Value = "True";
                    else
                        cmd.Parameters[2].Value = "False";
                    int process = cmd.ExecuteNonQuery();
                    if (con != null)
                        con.Close();
                    if (process > 0)
                    {
                        commentmanagermessage.Text = "Change Success";
                        UpdateData(statusTrue, statusFalse);
                    }
                    else
                        commentmanagermessage.Text = "Change Failed";
                }
            }
        }
    }
}